$(document).ready(function(){	
	$("#addUser").val(username);
	//下拉初始化
	InitParentCate();
	//父分类切换
	$("#cateParentName").change(function(){		
		var parentId=$(this).val();
		changeChildKv(parentId);
	});
	//子分类切换
	$("#cateChildName").change(function(){		
		var parentId=$(this).val();
		changeCateBrandIdKv(parentId);
	});
	
	//父品牌切换
	$("#brandParentName").change(function(){		
		var parentBrandId=$(this).val();
		changeParentBrandIdKv(parentBrandId);
	});
	//子品牌切换
	$("#brandChildName").change(function(){		
		var parentBrandId=$(this).val();
		changeProductModel(parentBrandId);
	});
//表单验证
var validate = $("#fromProductPic").validate({		   
		rules:{					
			picName:{
				  required:true,
			      remote: { 
	                   url: server_url+'/rest/ProductPic/checkPicName.do?timestamp='+Math.random()+ '&token=' + token,
	                   type: "post",
	                   dataType: "json", 
	                   data: {			                	     
	                	         picName: function() { return $("#picName").val();},
	                         } 
	               },
	               stringMaxLength:30
			},
			
			type:{
				   required:true,
			  },
			  cateChildName:{
				   required:true,
			  },
			  brandChildName:{
				   required:true,
			  },
			  cateName:{
				   required:true,
			  }
			 	
		},
		messages:{				
				picName:{
			      required:"该值不能为空！",
                  remote:"商品名已存在，请重新输入!",
                  stringMaxLength:"最多能输入30个字符！"
                 },
                 
                 type:{
                	 required:"该值不能为空！",
                 },
                 cateChildName:{
                	 required:"该值不能为空！",
                 },
                 brandChildName:{
                	 required:"该值不能为空！",
                 },
                 cateName:{
                	 required:"该值不能为空！",
                 },
                 
		},
		submitHandler:function(){
			sub();
		},
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo ( element.parent() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.parent() );
			else if ( element.is("input[name=captcha]") )
				error.appendTo ( element.parent() );
			else
				error.insertAfter(element);
		},
		validClass : 'rightValidate',
	    success: function(label) {
		   label.html("&nbsp;").text(" ") ;
	    }
 });
   

});


//初始化商品类型下拉列表
function InitParentCate()
{
	
	$.ajax({
		url:server_url+'/rest/productCate/selectParentCate.do?ran='+Math.random()+"&token="+token,
		type:'get',
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#cateParentName").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}
			}
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					      content : data.retMsg
					  },
					  autoClose : 2000
					});				
			} 			
		}
	});	
}
//切换子级分类
function changeChildKv(parentId){
	$.ajax({
		url:server_url+'/rest/productCate/selectChildCateKv.do?ran='+Math.random()+"&token="+token,
		type:'post',
		data:{cateId:parentId},
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#cateChildName").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}	
             }
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});			
			}
		}
	});
}
//切换子分类选择父品牌
function changeCateBrandIdKv(parentId){
	$.ajax({
		url:server_url+'/rest/ProductBrand/selectParentBrand.do?ran='+Math.random()+"&token="+token,
		type:'post',
		data:{cateId:parentId},
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#brandParentName").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}	
             }
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});			
			}
		}
	});
}

//切换父品牌选择子品牌
function changeParentBrandIdKv(parentBrandId){
	$.ajax({
		url:server_url+'/rest/ProductBrand/selectChildBrandKv.do?ran='+Math.random()+"&token="+token,
		type:'post',
		data:{brandId:parentBrandId},
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#brandChildName").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}	
             }
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});			
			}
		}
	});
}
//切换子品牌选择型号
function changeProductModel(parentBrandId){
	$.ajax({
		url:server_url+'/rest/ProductModel/selectParentBrand.do?ran='+Math.random()+"&token="+token,
		type:'post',
		data:{cateId:parentBrandId},
		success:function(data){
			if(data.retCode=="0000"){
				for(var i=0;i<data.data.length;i++){
					$("#cateName").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
				}	
             }
			else{
				easyDialog.open({
					  container : {
						  header : '提示',
					    content : data.retMsg
					  },
					  autoClose : 2000
					});			
			}
		}
	});
}
$("input[type=file]").change(function(){
	var id = $(this).attr("id");
	$.ajaxFileUpload({
		url : server_url+'/rest/fileUpload/uploadPic.do?token='+token+'&ran='+Math.random()+"&id="+$(this).attr("id"),
		type : 'post',
		contentType:"application/json",
		secureuri : false,  
		fileElementId:id,
		data:[$(this).val()],
		complete : function(orignal) {
			var reg = /<pre.*?>(.*?)<\/pre>/;
			var json = reg.exec(orignal["responseText"]);
			var data = eval("("+json[1]+")");
			console.log(data);
			if (data.retCode = "0000") {
				var path= data.data;
				$("#"+id).siblings("input[type=hidden]").val(path);
				$("#"+id).siblings("img").attr("src",path);
			}else{
				easyDialog.open({
					container:{
						header:"温馨提示",
						content:'上传失败'
					},
					noFn : true							
				});
			}
		},
		error: function(e) { 
			console.log(e);
		} 
	});
});
$("input[type=file]").change(function(){
	ajaxfileuploadUtil(this,"productPic");
});
//提交
function sub(){	
		if($("#fromProductPic").valid()){
			$.ajax({
				url:server_url+'/rest/ProductPic/ProductPicAdd.do?ran='+Math.random()+ '&token=' + token,
				type:'post',
				data:$("#fromProductPic").serialize(),
				success:function(data){
					easyDialog.open({container:{header:'温馨提示',content:data.retMsg},autoClose : 2000});
					back();
				}
			});
		}	
}

function back(){
	
	var url="productPicList.do?ran="+Math.random();
	tl_config.loadToWorkArea(url);
}

function submitCommit(){
	$("#fromProductPic").submit();
}

